package com.dsz.apibackend.controller;

import com.dsz.apibackend.common.BaseResponse;
import com.dsz.apibackend.common.ErrorCode;
import com.dsz.apibackend.common.ResultUtils;
import com.dsz.apibackend.exception.BusinessException;
import com.dsz.apibackend.model.dto.userInterfaceInfo.UserInterfaceInfoInvokeRequest;
import com.dsz.apibackend.service.UserInterfaceInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @author dushangzhi
 */
@RestController
@RequestMapping("/userInterfaceInfo")
@CrossOrigin(origins = "http://localhost:8000", allowCredentials = "true")
public class UserInterfaceInfoController {
    @Resource
    private UserInterfaceInfoService userInterfaceInfoService;

    /**
     * 接口调用次数+1
     */
    @PostMapping
    public BaseResponse<Boolean> invokeCount(@RequestBody UserInterfaceInfoInvokeRequest interfaceInfoInvokeRequest) {
        if (interfaceInfoInvokeRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long userId = Optional.ofNullable(interfaceInfoInvokeRequest.getUserId()).orElse(0L);
        Long interfaceInfoId = Optional.ofNullable(interfaceInfoInvokeRequest.getInterfaceInfoId()).orElse(0L);
        if (userId <= 0 || interfaceInfoId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = userInterfaceInfoService.invokeCount(userId, interfaceInfoId);
        return ResultUtils.success(res);
    }
}
